package com.zyz.M_File和IO流.IO流.节点流.InputStream和OutputStream;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * @author: vick
 * @Date: 2025/4/8
 * @Description:
 * @version: 1.0
 */
public class Demo3 {
    public static void main(String[] args) {

        FileInputStream fileInputStream = null;
        try {
            //1. 创建 File对象
            File file = new File("read.txt");

            //2. 创建流对象
            fileInputStream = new FileInputStream(file);
            //3. 定义变量，作为有效个数
            int len;
            //定义字节数组
            byte[] bytes = new byte[5];

            //循环读取
            while ((len = fileInputStream.read(bytes)) != -1) {
                //读取之后，将数组变成字符串打印
                String str = new String(bytes, 0, len);
                System.out.print(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //4. 关闭流
            try {
                if (fileInputStream != null) {
                    fileInputStream.close();
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
